<?php
// +----------------------------------------------------------------------
// | ADM Software [ 我宁可 为做过的事而后悔，也不想 因为没尝试而后悔。 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州安蝶梦电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.andiemeng.com>
// +----------------------------------------------------------------------
namespace Org\Util;
require_once 'httpful/bootstrap.php';

class AEHttp {
    
    private $_appkey="609388a15b3dfaca";
    private $_masterKey="1292b2d414d45c8f97d44354de24c40c";
    private $_databasename='api';
    private $_uriroot = "http://192.168.1.6:8080/api";
     /*
     * 构造函数,初始化需要连接的数据库
     */
    function __construct($databasename = 'api') { //初始化对象，将初始化值放在括号内
        $this->_databasename = $databasename;
        $aeurl=C('AEAPI_URL');
        if($aeurl){
            $this->_uriroot=$aeurl;
            $this->_appkey=C('AEAPI_APPKEY');
            $this->_masterKey=C('AEAPI_MASTERKEY');
        }
    }
    
    /*
     * 计算加密方法
     */
    function getSign($method,$param,$timestamp){
        $str="appkey=".$this->_appkey."&masterKey=".$this->_masterKey."&method=".$method."&param=".rawurlencode($param)."&timestamp=".$timestamp;  
        $str= str_replace("%27","'", $str);
        $str= str_replace("%28","(", $str);
        $str= str_replace("%29",")", $str);
        $str= str_replace("%2A","*", $str);
        $str= str_replace("%5C%2F","%2F", $str);  // "\/" 改成 "/"
        return md5($str);
        
    }
    
    /**
     * 批量查询 通过一组查询、排序、分页的条件筛选一组数据结果。
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function find($table='',$condition='',$limit=100,$skip=0,$sort='id-',$fields=''){
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".find";
        $body['method']=$method;
        $body['appkey']=$this->_appkey;
        $map['limit']=$limit;
        $map['skip']=$skip;
        $map['sort']=$sort;
        $map['table']=$table;
        $map['condition']=$condition;        
        if($fields !=''){
             $map['fields']=$fields;        
        }
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
     /**
     * 查询第一行符合条件的数据
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function first($table='',$condition='',$limit=100,$skip=0,$sort='id-'){
        $uri = $this->_uriroot; 
        $method= $this->_databasename.".first";
        $body['method']= $method;
        $body['appkey']=$this->_appkey; 
        $map['limit']=$limit;
        $map['skip']=$skip;
        $map['sort']=$sort;
        $map['table']=$table;
        $map['condition']=$condition;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
    
    /**
     * 通过get查询一条数据
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function get($table='',$id=''){
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".get";
        $body['method']=$method;
        $body['appkey']=$this->_appkey; 
        $map['table']=$table;
        $map['id']=$id;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
    /**
     * 通过ID删除一条数据
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function remove($table='',$id=''){
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".remove";
        $body['method']=$method;
        $body['appkey']=$this->_appkey;       
        $map['table']=$table;
        $map['id']=$id;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
     /**
     * 通过条件删除一组数据
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function clear($table='',$condition=''){
        
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".clear";
        $body['method']=$method;
        $body['appkey']=$this->_appkey;
        $body['timestamp']=$this->_timestamp;
        $body['sign']=$this->_sign; 
        $map['table']=$table;
        $map['condition']=$condition;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;        
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
    /**
     * 添加一条数据
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function create($table='',$row=''){
        
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".create";
        $body['method']=$method;
        $body['appkey']=$this->_appkey; 
        $map['table']=$table;
        $map['row']= $row;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;  
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
       
    }
    
    
    /**
     * 更新接口
     * @author 曹梦龙 <138888611@qq.com>
     */
    public function update($table='',$condition='',$row=''){
        
        $uri = $this->_uriroot; 
        $method=$this->_databasename.".update";
        $body['method']=$method; 
        $body['appkey']=$this->_appkey; 
        $map['table']=$table;
        $map['condition']=$condition;
        $map['row']= $row;        
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }
    
    /*
     * 通过筛选条件进行统计计数
     */
    public function count($table='',$condition=''){
        $uri = $this->_uriroot;
        $method=$this->_databasename.".count";
        $body['method']=$method;
        $body['appkey']=$this->_appkey;         
        $map['table']=$table;
        $map['condition']=$condition;          
        $body['param']=$map; 
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp; 
        $sgin=$this->getSign($method,json_encode($map,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin;
        $response = \Httpful\Request::post($uri)
                ->sendsJson()
                ->body($body)
                ->send();
        return $response->body;
    }

    /**
     * 调用自定义方法
     * @param $apiname
     * @param $param
     * @return mixed
     */
    public function postapi($apiname='',$param=array()){
        $uri = $this->_uriroot;
        $method=$this->_databasename.$apiname;
        $body['method']=$method;
        $body['appkey']=$this->_appkey; ;
        $body['param']=$param;
        $timestamp=NOW_TIME*1000;
        $body['timestamp']=$timestamp;
        $sgin=$this->getSign($method,json_encode($param,JSON_UNESCAPED_UNICODE),$timestamp);
        $body['sign']=$sgin; 
        $response = \Httpful\Request::post($uri)
            ->sendsJson()
            ->body($body)
            ->send();
        if($response->body->errno==0){
            //将自定义接口中data数据转成Array
           $result = json_decode(json_encode($response->body->data),TRUE);
        }else{
           $result=json_decode(json_encode($response->body),TRUE);
        }
        return $result;
    }

}
